<?php

function greengar_count_notification($senderid, $receiverid, $refererid, $after, $before, $type, $client_id) {
    $pairs = array();
    if ($senderid) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_SENDER_ID, 'value' => $senderid, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($receiverid) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_RECEIVER_ID, 'value' => $receiverid, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($refereid) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_REFERER_ID, 'value' => $refererid, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($after) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_TIMESTAMP, 'value' => $after, 'operand' => '>=', 'case_sensitive' => FALSE);
    }

    if ($before) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_TIMESTAMP, 'value' => $before, 'operand' => '<=', 'case_sensitive' => FALSE);
    }

    if ($type) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_TYPE, 'value' => $type, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($client_id) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_CLIENT_ID, 'value' => $client_id, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    $pairs[] = array('name' => ABSTRACT_NOTIFICATION_STATUS, 'value' => ABSTRACT_NOTIFICATION_STATUS_NEW, 'operand' => '=', 'case_sensitive' => FALSE);
    $options = array(
        'types' => FRIEND_NOTIFICATION_PARENT,
        'subtypes' => FRIEND_NOTIFICATION_SUBTYPE,
        'metadata_names' => NULL,
        'metadata_values' => NULL,
        'metadata_name_value_pairs' => $pairs,
        'metadata_name_value_pairs_operator' => 'AND',
        'metadata_case_sensitive' => FALSE,
        'order_by_metadata' => array(),
        'metadata_owner_guids' => ELGG_ENTITIES_ANY_VALUE,
        'count' => TRUE
    );
    $count = elgg_get_entities_from_metadata($options);
    return $count;
}

function greengar_get_notification($senderid, $receiverid, $refererid, $after, $before, $type, $client_id) {
    $pairs = array();
    if ($senderid !== NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_SENDER_ID, 'value' => $senderid, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($receiverid !== NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_RECEIVER_ID, 'value' => $receiverid, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($refereid !== NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_REFERER_ID, 'value' => $refererid, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($after !== NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_TIMESTAMP, 'value' => $after, 'operand' => '>=', 'case_sensitive' => FALSE);
    }

    if ($before !== NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_TIMESTAMP, 'value' => $before, 'operand' => '<=', 'case_sensitive' => FALSE);
    }

    if ($type != NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_TYPE, 'value' => $type, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    if ($client_id !== NULL) {
        $pairs[] = array('name' => ABSTRACT_NOTIFICATION_CLIENT_ID, 'value' => $client_id, 'operand' => '=', 'case_sensitive' => FALSE);
    }

    $pairs[] = array('name' => ABSTRACT_NOTIFICATION_STATUS, 'value' => ABSTRACT_NOTIFICATION_STATUS_NEW, 'operand' => '=', 'case_sensitive' => FALSE);
//    print_r($pairs);
    $options = array(
        'types' => FRIEND_NOTIFICATION_PARENT,
        'subtypes' => FRIEND_NOTIFICATION_SUBTYPE,
        'metadata_names' => NULL,
        'metadata_values' => NULL,
        'metadata_name_value_pairs' => $pairs,
        'metadata_name_value_pairs_operator' => 'AND',
        'metadata_case_sensitive' => FALSE,
        'order_by_metadata' => array(),
        'limit' => 10000,
        'metadata_owner_guids' => ELGG_ENTITIES_ANY_VALUE,
        'count' => FALSE
    );
    $entities = elgg_get_entities_from_metadata($options);
    return $entities;
}

?>